package com.crane.oss.web.controller.sports;


import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.SingleResponse;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.common.framework.utils.DateUtil;
import com.crane.oss.school.bean.request.QueryClassCountReq;
import com.crane.oss.school.service.CourseSetService;
import com.crane.oss.sports.XunFeiOpenCourseApiService;
import com.crane.oss.xunfei.req.QueryOpenCourseDetailClassReq;
import com.crane.oss.xunfei.req.QueryOpenCourseDetailTeacherReq;
import com.crane.oss.xunfei.req.QueryOpenCourseOverallReq;
import com.crane.oss.sports.bean.response.teachcourse.OpenDetailChartResp;
import com.crane.oss.sports.bean.response.teachcourse.OpenDetailInfoResp;
import com.crane.oss.sports.bean.response.teachcourse.OpenDetailStatisticResp;
import com.crane.oss.sports.bean.resquest.*;
import com.crane.oss.web.BaseBusinessController;
import com.crane.oss.xunfei.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(value = "TeachCourseController", tags = {"智慧体育-开课统计"})
@RestController
@RequestMapping("/sports/teachCourse")
public class TeachCourseController extends BaseBusinessController {

    @Resource
    XunFeiOpenCourseApiService xunFeiOpenCourseApiService;

    @Resource
    CourseSetService courseSetService;

    @ApiOperation(value = "总体概况-行政班开课总体情况", notes = "总体概况-行政班开课总体情况")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryOpenDetailStatisticReq")
    @PostMapping("/open/detail/statistic")
    public SingleResponse<OpenDetailStatisticResp> openDetailStatistic(@RequestBody QueryOpenDetailStatisticReq request) {
        OpenDetailStatisticResp openDetailStatisticResp=new OpenDetailStatisticResp();

        QueryOpenCourseOverallReq queryOpenCourseOverallReq=new QueryOpenCourseOverallReq();
        queryOpenCourseOverallReq.setFlag(request.getFlag());
        if(request.getFlag() ==0){
            queryOpenCourseOverallReq.setTime(DateUtil.formatDateTime(DateUtil.DATE_PATTERN_2,request.getBeginTime())+"-"+DateUtil.formatDateTime(DateUtil.DATE_PATTERN_2,request.getEndTime()));
        }else  if(request.getFlag() ==1){
            queryOpenCourseOverallReq.setCycleId(request.getCycleId());
        }
        QueryClassCountReq queryClassCountReq=new QueryClassCountReq();
        BeanUtil.copyProperties(request,queryClassCountReq);
        queryClassCountReq.setPartyId(87L);
        int count=courseSetService.queryClassArangementCount(queryClassCountReq);
        queryOpenCourseOverallReq.setClassType(request.getClassType());
        ApiSingleResponse<OpenCourseOverallResp>  response=xunFeiOpenCourseApiService.getOpenCourseOverall(queryOpenCourseOverallReq,getPartyId().toString(),null,getUserId().toString());
        if(null != response.getMsg() && response.getMsg().equals("success")){
            openDetailStatisticResp.setTargetCourse(response.getData().getTargetCourse());
            openDetailStatisticResp.setAvgTime(response.getData().getAvgTime());
            openDetailStatisticResp.setCourseCount(response.getData().getCourseCount());
            openDetailStatisticResp.setAvgCount(response.getData().getAvgCount());
            openDetailStatisticResp.setOpenRate(response.getData().getOpenRate());
        }
        return SingleResponse.of(openDetailStatisticResp);
    }


    @ApiOperation(value = "总体概况-图表列表", notes = "总体概况-图表列表")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryOpenDetailStatisticReq")
    @PostMapping("/open/detail/chart")
    public MultiResponse<OpenDetailChartResp> openDetailChart(@RequestBody QueryOpenDetailStatisticReq request) {
        QueryOpenCourseOverallReq queryOpenCourseOverallReq=new QueryOpenCourseOverallReq();
        queryOpenCourseOverallReq.setFlag(request.getFlag());
        if(request.getFlag() ==0){
            queryOpenCourseOverallReq.setTime(DateUtil.formatDateTime(DateUtil.DATE_PATTERN_2,request.getBeginTime())+"-"+DateUtil.formatDateTime(DateUtil.DATE_PATTERN_2,request.getEndTime()));
        }else  if(request.getFlag() ==1){
            queryOpenCourseOverallReq.setCycleId(request.getCycleId());
        }
        queryOpenCourseOverallReq.setClassType(request.getClassType());
        ApiListResponse<OpenCourseSituationResp> response=xunFeiOpenCourseApiService.getOpenCourseSituation(queryOpenCourseOverallReq,getPartyId().toString(),null,getUserId().toString());
        if(null != response.getMsg() && response.getMsg().equals("success")){
             List<OpenDetailChartResp> respList=response.getData().stream().map(s ->{
                 OpenDetailChartResp a=new OpenDetailChartResp();
                 a.setAvgTime(s.getAvgTime());
                 a.setCourseCount(s.getCourseCount());
                 a.setOpenRate(s.getOpenRate());
                 a.setClassName(s.getClassName());
                 return  a;
             }).collect(Collectors.toList());
            return MultiResponse.ofWithoutTotal(respList);
        }
        return MultiResponse.ofWithoutTotal(new ArrayList<>());
    }

    @ApiOperation(value = "班级详情-开课详情", notes = "班级详情-开课详情")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryClassOpenDetailReq")
    @PostMapping("/open/class/coursedetail")
    public MultiResponse<OpenDetailInfoResp> openClassCourseDetail(@RequestBody QueryClassOpenDetailReq request) {

        QueryOpenCourseDetailClassReq queryOpenCourseDetailClassReq=new QueryOpenCourseDetailClassReq();
        queryOpenCourseDetailClassReq.setTime(request.getMonth());
        queryOpenCourseDetailClassReq.setClassId(request.getClassId());
        queryOpenCourseDetailClassReq.setPage(request.getPageNo());
        queryOpenCourseDetailClassReq.setLimit(request.getPageSize());
        ApiListResponse<OpenCourseDetailClassResp> response=xunFeiOpenCourseApiService.getOpenCourseDetailClass(queryOpenCourseDetailClassReq,getPartyId().toString(),null,getUserId().toString());
        if(null != response.getMsg() && response.getMsg().equals("success")){
            List<OpenDetailInfoResp> respList=response.getData().stream().map(s ->{
                OpenDetailInfoResp a=new OpenDetailInfoResp();
                a.setBTime(s.getBtime());
                a.setTeacherName(s.getTeacherName());
                a.setCampusName(s.getCampusName());
                a.setETime(s.getEtime());
                a.setClassName(s.getClassName());
                a.setBeginTime(s.getBeginTime());
                a.setEndTime(s.getEndTime());
                a.setTime(s.getTime());
                a.setDayTime(s.getDayTime());
                a.setDura(s.getDura());
                a.setCourseId(s.getCourseId());
                return  a;
            }).collect(Collectors.toList());
            return MultiResponse.of(respList,response.getTotal(),request);
        }
        return MultiResponse.of(new ArrayList<>(),0,request);
    }

    @ApiOperation(value = "教师详情-开课详情", notes = "教师详情-开课详情")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryTeacherOpenDetailReq")
    @PostMapping("/open/teacher/courseDetail")
    public MultiResponse<OpenDetailInfoResp> openTeacherCourseDetail(@RequestBody QueryTeacherOpenDetailReq request) {
        QueryOpenCourseDetailTeacherReq queryOpenCourseDetailTeacherReq=new QueryOpenCourseDetailTeacherReq();
        queryOpenCourseDetailTeacherReq.setTeacherId(request.getTeacherId());
        queryOpenCourseDetailTeacherReq.setBeginTime(request.getBeginTime().getTime());
        queryOpenCourseDetailTeacherReq.setEndTime(request.getEndTime().getTime());
        queryOpenCourseDetailTeacherReq.setPage(request.getPageNo());
        queryOpenCourseDetailTeacherReq.setLimit(request.getPageSize());
        ApiListResponse<OpenCourseDetailTeacherResp> response=xunFeiOpenCourseApiService.getOpenCourseDetailTeacher(queryOpenCourseDetailTeacherReq,getPartyId().toString(),null,getUserId().toString());
        if(null != response.getMsg() && response.getMsg().equals("success")){
            List<OpenDetailInfoResp> respList=response.getData().stream().map(s ->{
                OpenDetailInfoResp a=new OpenDetailInfoResp();
                a.setBTime(s.getBtime());
                a.setTeacherName(s.getTeacherName());
                a.setCampusName(s.getCampusName());
                a.setETime(s.getEtime());
                a.setClassName(s.getClassName());
                a.setBeginTime(s.getBeginTime());
                a.setEndTime(s.getEndTime());
                a.setTime(s.getTime());
                a.setDayTime(s.getDayTime());
                a.setDura(s.getDura());
                return  a;
            }).collect(Collectors.toList());
            return MultiResponse.of(respList,response.getTotal(),request);
        }
        return MultiResponse.of(new ArrayList<>(),0,request);
    }

}
